var rootPath = getRootPath();

$(function() {
	$.ajax({
		url : rootPath + "/school/queryCurSchoolInfo",
		method : "GET",
		data : {
			"state" : 2,
			"dr" : 1
		},
		success : function(data) {
			if(data.returnCode == '0000') {
				console.log(data);
				var school = data.returnObject;
				$("#sid").html(school.sid);
				$("#sname").html(school.sname);
				$("#address").html(formatUndefined(school.schoolProvince) + formatUndefined(school.schoolCity) + formatUndefined(school.schoolArea));
				if(school.director != null) {
					$("#duid").val(school.director.uid);
					$("#director").html(school.director.realName);
				}
			} else {
				layer.alert(data.returnMessage);
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			layer.alert("error");
		}
	});
	
	$("#reBindSchoolMaster,#reBindDirector").click(function() {
		//加载当前学校的梦想教师
		$.ajax({
			url : rootPath + "/school/queryCurSchoolTeachers",
			method : "GET",
			data : {
				"state" : 2,
				"dr" : 1
			},
			success : function(data) {
				if(data.returnCode == '0000') {
					var schoolTeachers = data.returnObject;
					var length = schoolTeachers.length;
					var div = "";
					for (var i = 0; i < length; i++) {
						div += "<div class='teacher' data-uid='" + schoolTeachers[i].uid + "'>" + schoolTeachers[i].realName + "</div>";
					}
					$("#schoolTeachers").empty();
					$("#schoolTeachers").append(div);
					popBox();
					$(".teacher").each(function(e) {
						$(this).click(function(e) {
							var uid = $(this).data("uid");
							var teacher = $(this).html();
							$("#duid").val(uid);
							$("#director").html(teacher);
							closeBox();
						});
					});
				} else if(data.returnCode == '2001'){
					layer.alert('学校暂无梦想教师,请通知老师进行梦想教师认证');
				} else {
					layer.alert(data.returnMessage);
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				layer.alert("error");
			}
		});
	});
	//点击关闭图片
	$("#closePic").click(closeBox);
});

/* 点击弹出按钮 */
function popBox() {
	$("#popBox").css("display", "block");
	$("#popLayer").css("display", "block");
	
	//解决'弹出层出现时,body还能滚动'的bug
	document.documentElement.style.height = '100%';
	document.documentElement.style.overflow = 'hidden';
	document.body.style.height = '100%';
	document.body.style.overflow = 'hidden';
};

//隐藏背景层和弹出层
function closeBox() {
	$("#popBox").css("display", "none");
	$("#popLayer").css("display", "none");
	$("body").css({"overflow":"auto"});
}